<template>
  <div class="login-container">
    <el-row :gutter="20">
      <el-col :span="10"><div class="loginLayout"></div></el-col>
      <el-col :span="10"><div class="loginLayout"></div></el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8"><div class="loginLayout"></div></el-col>
      <el-col :span="8">
        <div class="login-box" style="position: center;background-color: white;padding: 10px;border-radius:10px;box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);">
          <h3>商城登录</h3>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="account">
              <el-input v-model="ruleForm.account" clearable></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item style="text-align: right">
              <el-button type="primary" @click="submitForm('ruleForm')"size="small">登录</el-button>
              <el-button @click="resetForm('ruleForm')" size="small">重置</el-button>
              <el-button @click="registerOpen"size="small">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="8"><div class="loginLayout"></div></el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="10"><div class="loginLayout"></div></el-col>
      <el-col :span="10"><div class="loginLayout"></div></el-col>
    </el-row>
    <el-dialog title="账户注册" :visible.sync="dialogTableVisible" width="400px" :modal-append-to-body="false">
      <el-form ref="registerForm" :model="registerForm" :rules="rules2">
        <el-form-item label="账户" prop="account">
          <el-input v-model="registerForm.account"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="name">
          <el-input v-model="registerForm.name"></el-input>
        </el-form-item>
<!--        <el-form-item label="注册类型" prop="role">-->
<!--          <el-select v-model="registerForm.role" placeholder="请选择注册类型">-->
<!--            <el-option label="普通用户" value="1"></el-option>-->
<!--            <el-option label="管理员" value="2"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="registerForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model.number="registerForm.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register('registerForm')">立即注册</el-button>
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-top: 20px">
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.checkPass !== '') {
          this.$refs.registerForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        account: '',
        password: '',
      },
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      rules2:{
        account:[
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        name:[
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        role:[
          {required: true, message: '请选择注册类型', trigger: 'blur' }
        ],
        password: [
          { required: true,validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true,validator: validatePass2, trigger: 'blur' }
        ],
        phone:[
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      registerForm: {
        account:'',
        name:'',
        password:'',
        checkPass:'',
        phone:'',
        role:'1'
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this=this;
      const accountP=this.ruleForm.account;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/login',this.ruleForm).then(function (resp){
            if(resp.data=='1'){
              _this.$store.dispatch('login',accountP);
              _this.$message({
                message: '用户登录成功',
                type: 'success'
              });
              _this.$router.push('/userIndex');//跳转页面
              sessionStorage.setItem("store",JSON.stringify(this.$store.state));
            }else if(resp.data=='2'){
              _this.$store.dispatch('login',accountP);
              _this.$message({
                message: '管理员登录成功',
                type: 'success'
              });
              _this.$router.push('/adminIndex1');//跳转页面
              sessionStorage.setItem("store",JSON.stringify(this.$store.state));
            }else{
              _this.$message({
                message: '密码或用户名错误',
                type: 'warning'
              });
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    registerOpen(){
      this.dialogTableVisible = true
    },
    register(formName){
      const _this=this;
      console.log(this.registerForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/user/register',this.registerForm).then(function (resp){
            if(resp.data=='1'){
              _this.$message({
                message: '注册成功',
                type: 'success'
              });
            }else{
              _this.$message({
                message: '注册失败',
                type: 'warning'
              });
            }
          })
        }
      });
    },
  }
}
</script>

<style scoped>
.login-container{
  background: url("../assets/loginBackground2.png") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}
.loginLayout{
  height: 120px;
}
</style>